package com.xxl.job.executor.jobhandler.impl.integral;

import com.alibaba.fastjson.JSONObject;
import com.xxl.job.executor.jobhandler.impl.EveryDayExecute;
import com.xxl.job.sscIntegrInfo.domain.SscIntegrInfoVo;
import com.xxl.job.sscIntegrInfo.service.ISscIntegrInfoService;
import com.xxl.job.sscMemberSysmessage.domain.SscMemberSysmessage;
import com.xxl.job.sscMemberSysmessage.service.ISscMemberSysmessageService;
import com.xxl.job.sysStoreUserMessagePush.domain.SysStoreUserMessagePush;
import com.xxl.job.sysStoreUserMessagePush.service.ISysStoreUserMessagePushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;

/**
 * @author : zyc
 * @version : 1.0
 * @create : 2023/09/01 11:30
 **/
@Component
public class IntAdventRemindExecute implements EveryDayExecute {

    private static final Logger log = LoggerFactory.getLogger(IntAdventRemindExecute.class);

    @Autowired
    private ISscIntegrInfoService iSscIntegrInfoService;

    @Autowired
    private ISscMemberSysmessageService iSscMemberSysmessageService;

    @Autowired
    private ISysStoreUserMessagePushService iSysStoreUserMessagePushService;

    /**
      * 任务积分临期提醒
      *
      * @author: zyc
      * @date: 2023/9/1 11:31
      */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void execute() {
        try {
            // 临期天数
            Integer adventDay = 3;

            SscIntegrInfoVo infoVo = new SscIntegrInfoVo();
            infoVo.setAdventDay(adventDay);
            List<SscIntegrInfoVo> list = iSscIntegrInfoService.selectIntegralAdventList(infoVo);
            for (SscIntegrInfoVo fIntegral : list) {
                // 数据封装
                HashMap<String, Object> pcMaps = new HashMap<>();
                pcMaps.put("businessDescribe", "用户临期积分提醒");
                pcMaps.put("businessType", "adventIntegral");
                pcMaps.put("memberId", fIntegral.getMemberId());
                pcMaps.put("adventDay", adventDay);
                pcMaps.put("closeDate", fIntegral.getCloseDate());
                pcMaps.put("advTotalIntegral", fIntegral.getAdvTotalIntegral());

                // 新增消息推送
                SysStoreUserMessagePush iMsgPush = new SysStoreUserMessagePush();
                iMsgPush.setMsgType("3");
                iMsgPush.setIsOfficial("11");
                iMsgPush.setPushContent(JSONObject.toJSONString(JSONObject.parseObject(JSONObject.toJSONString(pcMaps))));
                iMsgPush.setCreateBy("admin");
                iSysStoreUserMessagePushService.insertSysStoreUserMessagePush(iMsgPush);

                // 新增会员接收消息推送
                SscMemberSysmessage iMemberSys = new SscMemberSysmessage();
                iMemberSys.setSysPushId(iMsgPush.getSysPushId());
                iMemberSys.setReceiveMemberId(fIntegral.getMemberId());
                iMemberSys.setCreateBy("admin");
                iSscMemberSysmessageService.insertSscMemberSysMessage(iMemberSys);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("处理任务积分临期提醒出现异常 : " + e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
}
